package com.auerdi.ui.account.view;

import android.content.Context;
import android.text.Editable;
import android.text.NoCopySpan;
import android.text.TextWatcher;
import android.util.AttributeSet;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.inputmethod.EditorInfo;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.auerdi.R;
import com.example.egghttp.EggLog.L;


/**
 * Android自定义SearchView
 * Created by yuandl on 2016-11-17.
 */

public class NumView extends LinearLayout implements TextWatcher, View.OnClickListener {


    /**
     * 输入框
     */
    private EditText et_search;
    /**
     * 输入框后面的那个清除按钮
     */
    private Button bt_clear;


    public NumView(Context context, AttributeSet attrs) {
        super(context, attrs);
        /**加载布局文件*/
        LayoutInflater.from(context).inflate(R.layout.text_input_searchview_b, this, true);
        /***找出控件*/
        et_search = (EditText) findViewById(R.id.et_search);
        bt_clear = (Button) findViewById(R.id.bt_clear);
        bt_clear.setVisibility(GONE);
        et_search.addTextChangedListener(this);
        bt_clear.setOnClickListener(this);


//        et_search.setFocusable(true);
//        et_search.setFocusableInTouchMode(true);
//        et_search.requestFocus();


        et_search.setOnEditorActionListener(new TextView.OnEditorActionListener() {

            @Override
            public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
                //当actionId == XX_SEND 或者 XX_DONE时都触发
                //或者event.getKeyCode == ENTER 且 event.getAction == ACTION_DOWN时也触发
                //注意，这是一定要判断event != null。因为在某些输入法上会返回null。
                L.d("hcia", "event:" + event + " actionId:" + actionId + " text:" + et_search.getText() + "");


                if (actionId == EditorInfo.IME_ACTION_SEARCH) {

                }
                if (actionId == EditorInfo.IME_ACTION_SEARCH) {
                    textWatcher.afterTextChanged(et_search.getText() + "");

                }

                if (actionId == EditorInfo.IME_ACTION_SEARCH
                        || actionId == EditorInfo.IME_ACTION_DONE
                        || (event != null && KeyEvent.KEYCODE_ENTER == event.getKeyCode() && KeyEvent.ACTION_DOWN == event.getAction())) {
//                    L.d("hcia", "event:" + event + " actionId:" + actionId + " text:" + et_search.getText() + "");

//                    et_search.setText("");
                }
                return false;
            }
        });

    }


    @Override
    public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

    }

    @Override
    public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {

    }

    /****
     * 对用户输入文字的监听
     *
     * @param editable
     */
    @Override
    public void afterTextChanged(Editable editable) {
        /**获取输入文字**/
        String input = et_search.getText().toString().trim();
        if (input.isEmpty()) {
            bt_clear.setVisibility(GONE);
        } else {
            bt_clear.setVisibility(VISIBLE);
        }

    }

    @Override
    public void onClick(View view) {
        et_search.setText("");
    }


    private SearchWatcher textWatcher;

    public void setTextWatcher(SearchWatcher textWatcher) {
        this.textWatcher = textWatcher;
    }

    public String getText() {

        return et_search.getText()+"";
    }



    public interface SearchWatcher extends NoCopySpan {

        public void afterTextChanged(String barcode);
    }

    public void setHint(String hint) {


        et_search.setHint(hint);

    }

    public EditText getEditText() {
        return et_search;
    }

}
